﻿using System;
using System.Collections.Generic; 
using System.Text;
using ATSIDAL;
using ATSModel;
using System.Data.SqlClient;
using System.Data;

namespace ATSDAL
{
    public class TPrintRecordDal : BaseRepository_DT<TPrintRecordInfo>, ITPrintRecord
    {
        #region 变量
        private const string PRINT_RECORD_PRODUCTSN = "@ProductSN";
        private const string PRINT_RECORD_MOID = "@MOID";
        private const string PRINT_RECORD_RECORDTIME = "@RecordTime";
        #endregion


        /// <summary>
        /// 根据时间检索标签打印记录
        /// </summary>
        /// <param name="starttime">开始时间</param>
        /// <param name="stoptime">结束时间</param>
        /// <returns></returns>
        public IList<TPrintRecordInfo> GetPrintRecordbyDT(DateTime starttime, DateTime stoptime)
        {
            SqlParameter[] prParms = GetDTParameters();
            prParms[0].Value = starttime;
            prParms[1].Value = stoptime;
            return ExcuteSqlToGetEntities("sp_selPrintRecordByDate", prParms);
        }


        /// <summary>
        /// 根据成品编码 检索标签打印记录
        /// </summary>
        /// <param name="productSN">成品编码</param>
        /// <returns></returns>
        bool i;
        public IList<TPrintRecordInfo> GetPrintRecordbySN(string productSN)
        {
            i = false;
            SqlParameter parm = GetParameter(PRINT_RECORD_PRODUCTSN);
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("sp_selPrintRecordByProductSN", parm);
        }

        /// <summary>
        /// 根据MO 检索标签打印记录
        /// </summary>
        /// <param name="MO">MO号</param>
        /// <returns></returns>
        public IList<TPrintRecordInfo> GetPrintRecordbyMO(string MO)
        {
            i = true;
            SqlParameter parm = GetParameter(PRINT_RECORD_MOID);
            parm.Value = MO;
            return ExcuteSqlToGetEntities("sp_selPrintRecordByMO", parm);
        }


       //重写基础类
        public override TPrintRecordInfo GetEntityRecord(SqlDataReader rdr)
        {
            if (i!=true)
            {
                TPrintRecordInfo pro = new TPrintRecordInfo
                {

                    ProductSN = rdr.GetValue(0).ToString(),
                    PCIP = rdr.GetValue(1).ToString(),
                    RecordTime = rdr.GetDateTime(2),
                    UserId = rdr.GetValue(3).ToString(),
                    TempName = rdr.GetValue(4).ToString(),
                    Remark = rdr.GetValue(5).ToString()

                };
                return pro;
            }
            else 
            {
                TPrintRecordInfo pro = new TPrintRecordInfo
                {
                    MOID = rdr.GetValue(0).ToString(),
                    ProductSN = rdr.GetValue(1).ToString(),
                    PCIP = rdr.IsDBNull(2) ? null : rdr.GetString(2),
                    RecordTime =rdr.IsDBNull(3)? null:(DateTime?)rdr.GetDateTime(3), 
                    UserId = rdr.IsDBNull(4) ? null : rdr.GetString(4),
                    TempName = rdr.IsDBNull(5) ? null : rdr.GetString(5),
                    Remark = rdr.IsDBNull(6) ? null : rdr.GetString(6), //rdr.GetValue(6).ToString(),
                    IsPrint = rdr.GetValue(7).ToString() 
                };
                return pro;
            }
          
        } 
        
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="parmname"></param>
        /// <returns></returns>
        private static SqlParameter GetParameter(string parmname)
        {
            SqlParameter parm = new SqlParameter(parmname, SqlDbType.NVarChar, 50);
            return parm;
        }


        #region IBaseRepository<TPrintRecordInfo> 成员


        public TPrintRecordInfo LoadEntity<T1>(T1 Id)
        {
            throw new NotImplementedException();
        }

        public IList<TPrintRecordInfo> LoadEntities()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
